define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
    let course_id = $('#course_id').val();
    var Controller = {
        index: function () {

            $('.btn-add').data("area",["80%","100%"]);
            $('.btn-edit').data("area",["80%","100%"]);

            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'question/index' + location.search,
                    add_url: 'question/add/course_id/' + course_id,
                    edit_url: 'question/edit',
                    del_url: 'question/del',
                    multi_url: 'question/multi',
                    import_url: 'question/import',
                    table: 'course_question',
                }
            });

            var table = $("#table");

            table.on('post-common-search.bs.table', function (event, table) {
                var form = $("form", table.$commonsearch);
                $("input[name='course_id']", form).addClass("selectpage").data("source", "course/index").data("primaryKey", "id").data("field", "name").data("orderBy", "id desc").data("pageSize", 10);
                $("input[name='extend_id']", form).addClass("selectpage").data("source", "coursepaper/index").data("primaryKey", "id").data("field", "name").data("orderBy", "id desc").data("pageSize", 10);
                Form.events.cxselect(form);
                Form.events.selectpage(form);
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                escape:false,
                searchFormVisible:true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'question_name',
                            title: __('Question_name'),
                            cellStyle: function (value, row, index) {
                                let css = {
                                    css: {
                                        "min-width": "150px",
                                        "white-space": "nowrap",
                                        "text-overflow": "ellipsis",
                                        "overflow": "hidden",
                                        "max-width": "300px"
                                    }
                                };
                                return css;
                            }
                        },
                        {
                            field: 'question_type',
                            title: __('Question_type'),
                            searchable: true,
                            formatter: Table.api.formatter.normal,
                            searchList: {
                                0: __('-'),
                                1: __('单选'),
                                2: __('多选'),
                                3: __('判断'),
                                4: __('问答'),
                                5: __('填空'),
                                6: __('完形填空'),
                                7: __('阅读理解'),
                                8: __('名词解释'),
                            }
                        },
                        {
                            field: 'course_id',
                            title: __('所属课程'),
                            visible: false
                        },
                        {
                            field: 'course.name',
                            title: __('所属课程'),
                            searchable:false
                        },
                        {
                            field: 'extend_id',
                            title: __('试卷名称'),
                            visible: false,
                            searchable: true,
                            renderDefault: true
                        },
                        // {
                        //     field: 'charge_type',
                        //     title: __('Charge_type'),
                        //     custom: {"0": "primary", "1": "primary", "2": "primary"},
                        //     formatter: Table.api.formatter.normal,
                        //     searchList: {0: __('-'), 1: __('免费'), 2: __('收费')}
                        // },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            table.on('post-body.bs.table',function () {
                $('.btn-editone').data("area",["80%","100%"]);
            });

            // 动态修改请求参数
            table.bootstrapTable('getOptions').extend.add_url = 'question/add/course_id/' + $('#course_id').val();


            // 监听导入题库事件
            $('.btn-add-question').click(function (){
                var url = 'question/importQuestion';
                Fast.api.open(url, $(this).data("original-title") || $(this).attr("title") || __('导入题目'), $(this).data() || {}, {
                    success: function () {
                        $(".btn-refresh").trigger("click");
                    }
                });
            });

            // 监听导出题库事件
            $('.btn-export-question').click(function (){
                var url = 'question/exportQuestion';
                Fast.api.open(url, $(this).data("original-title") || $(this).attr("title") || __('导出题目'), $(this).data() || {}, {
                    success: function () {
                        $(".btn-refresh").trigger("click");
                    }
                });
            });

            // 监听清空题目事件
            $('.btn-clear-question').click(function () {
                var url = 'question/clearQuestion';
                Fast.api.open(url, $(this).data("original-title") || $(this).attr("title") || __('清空题目'), $(this).data() || {}, {
                    success: function () {
                        $(".btn-refresh").trigger("click");
                    }
                });
            });

            // 监听题目查重事件
            $('.btn-repeat-question').click(function (){
                var courseId = $('#course_id').val();
                var url = 'question/repeatQuestion/ids/'+courseId;
                Fast.api.open(url, $(this).data("original-title") || $(this).attr("title") || __('题目查重'), {
                    callback: function(data){
                        console.log(data);
                        $("#id").val(data);
                        table.bootstrapTable('getOptions').sortName = 'question_name';
                        $(".btn-refresh").trigger("click");
                    }
                });
            });
        },
        add: function () {

            // 生成默认表单
            Controller.buildQuestionInput({'action':1});

            // 监听课程变动,加载此课程的试卷
            $("#c-paper_id").data("params", function (obj) {
                return {custom: {course_id: $("#c-course_id").val()}};
            });

            // 监听题目类型变动
            $("#add-form").on('change',"select[name='row[question_type]']",function (){
                var question_type = $(this).val();
                Controller.buildQuestionInput({'action':1,'question_type':question_type});
            });
        },
        edit: function () {
            // 发起请求
            let id = $('#question_id').val();
            Fast.api.ajax({
                url: 'question/buildHtml/id/' + id + '/action/2',
                loading: false,
            }, function (ret) {
                $('#question_build_html').html(ret);
                Controller.api.bindevent();
                return false;
            });

            // 监听题目类型变动
            $("#edit-form").on('change',"select[name='row[question_type]']",function (){
                var question_type = $(this).val();
                Controller.buildQuestionInput({'id':id,'action':2,'question_type':question_type});
            });
        },
        importquestion:function (){
            $("#c-paper_id").data("params", function (obj) {
                return {custom: {course_id: $("#c-course_id").val()}};
            });
            Controller.api.bindevent();
        },
        exportquestion:function (){
            $("#c-paper_id").data("params", function (obj) {
                return {custom: {course_id: $("#c-course_id").val()}};
            });
            Controller.api.bindevent();
        },
        clearquestion:function (){
            $("#c-paper_id").data("params", function (obj) {
                return {custom: {course_id: $("#c-course_id").val()}};
            });
            Controller.api.bindevent();
        },
        repeatquestion:function (){
            Controller.api.bindevent();
            Form.api.bindevent("form[role=form]", function(data, ret){
                Fast.api.close(data);
                return false;
            });
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        },
        buildQuestionInput: function (data){
            Fast.api.ajax({
                url: 'question/buildHtml',
                data:data,
                loading: false,
            }, function (ret) {
                $('#question_build_html').html(ret);
                Controller.api.bindevent();
                return false;
            });
        }
    };
    return Controller;
});
